Imports System.IO

Public Class ClassCompilerSetup
    Enum compilerType As Integer
        ACME = 1
        AS65
        DASM
        TASS64
        SNASM
        WLA6510
        LAST
    End Enum
    Public Class buildConfig
        Public description As String
        Private exeName As String
        Private compilerBuildFlags As String
        Private succesCode As Integer
    End Class

    Private compilerPath As String
    Private compilerBuildFlags As String
    Private emuPath As String
    Private emuFlags As String
    Private Shared compilerTypeSet As Integer
    Private succesCode As Integer
    'Private compilerBuildFlags As String

    'Public Shared buildSetups() As buildConfig = { _
    '    {"ACME", "acme.exe", " ", 0}, _
    '    {"AS65", "as65.exe", " ", 0}, _
    '    {"DASM Macro Assembler", "dasm.exe", " ", Integer.MinValue}, _
    '    {"TASS64", "64tass.exe", " ", 0}, _
    '    {"SNASM", "snasm.exe", " ", 0} _
    '}
    Public Shared assemblerDesc() As String = { _
    "ACME", "AS65", "DASM", "TASS64", "SNASM", "WLA-6510" _
}

    Public Function invokeCompiler(ByRef tb As RichTextBox, ByVal sourceFileName As String) As String
        Dim compiler As New Process
        Dim defaultExtension As String = "prg"

        If sourceFileName = "" Then
            sourceFileName = "noname.asm"
        End If

        Dim outputFileName As String = Path.ChangeExtension(sourceFileName, defaultExtension)
        Dim succesCode As Integer = 0

        compilerPath = Path.GetDirectoryName(Application.ExecutablePath)

        Dim selectedCompilerType As Integer = My.Settings.assemblerSelected
        Dim flag1 As String
        Dim flag2 As String
        Dim flag3 As String

        If sourceFileName.Contains(" ") Then
            sourceFileName = Chr(34) & sourceFileName & Chr(34)
        End If
        If outputFileName.Contains(" ") Then
            outputFileName = Chr(34) & outputFileName & Chr(34)
        End If

        Select Case selectedCompilerType
            Case compilerType.ACME
                compilerPath = Path.Combine(compilerPath, "acme.exe")
                compilerBuildFlags = "-o " & outputFileName & " " & sourceFileName
                succesCode = 0
            Case compilerType.AS65
                compilerPath = Path.Combine(compilerPath, "as65.exe")
                compilerBuildFlags = sourceFileName + " -i -n"
                succesCode = 0
            Case compilerType.DASM
                compilerPath = Path.Combine(compilerPath, "dasm.exe")
                compilerBuildFlags = sourceFileName + " -o" + outputFileName + " -v2 -p3"
                succesCode = Integer.MinValue
            Case compilerType.TASS64
                compilerPath = Path.Combine(compilerPath, "64tass.exe")
                compilerBuildFlags = "--version --m6502 -o" + outputFileName + " " + sourceFileName
                succesCode = 0
            Case compilerType.SNASM
                compilerPath = Path.Combine(compilerPath, "snasm.exe")
                compilerBuildFlags = sourceFileName + " " + outputFileName
                succesCode = 0
            Case compilerType.WLA6510
                compilerPath = Path.Combine(compilerPath, "wla-6510.exe")
                compilerBuildFlags = "-vo " + sourceFileName + " " + outputFileName
                succesCode = 0
        End Select

        tb.Clear()
        tb.AppendText("Working folder is: " + CurDir() + vbCrLf)
        tb.AppendText("Using: " + compilerPath + vbCrLf)
        tb.AppendText("Building...: " + sourceFileName + vbCrLf)

        compiler.StartInfo.FileName = compilerPath
        compiler.StartInfo.Arguments = compilerBuildFlags

        tb.AppendText("Command: " & Path.GetFileName(compilerPath) + " " + compilerBuildFlags & vbCrLf)

        compiler.StartInfo.UseShellExecute = False
        compiler.StartInfo.RedirectStandardOutput = True
        compiler.StartInfo.RedirectStandardError = True
        Try
            compiler.Start()
            tb.AppendText(compiler.StandardOutput.ReadToEnd())
            tb.AppendText(compiler.StandardError.ReadToEnd())
            compiler.WaitForExit()
            If succesCode <> Integer.MinValue Then
                If compiler.ExitCode = succesCode Then
                    tb.AppendText("Build succeeded." + vbCrLf)
                Else
                    tb.AppendText("Build failed!" + vbCrLf)
                    outputFileName = ""
                End If
            Else
                tb.AppendText("Build process exited with code: " + CStr(compiler.ExitCode) + vbCrLf)
            End If
            tb.ScrollToCaret()

            compiler.Dispose()

            Return outputFileName
        Catch ex As Exception
            tb.AppendText("Couldn't start build process:" & ex.Message & vbNewLine)
        End Try
        Return ""

    End Function

    Public Sub invokeEmu(ByRef tb As RichTextBox, ByVal objectFileName As String)
        Dim emu As New Process
        Dim emuPath As String = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), _
            My.Settings.emulatorExe)

        tb.AppendText("Running...: " + objectFileName + vbCrLf)
        tb.ScrollToCaret()

        emu.StartInfo.FileName = emuPath
        emu.StartInfo.Arguments = " " & objectFileName

        tb.AppendText("Command: " & Path.GetFileName(emuPath) + emu.StartInfo.Arguments & vbCrLf)

        emu.StartInfo.UseShellExecute = True
        'emu.StartInfo.RedirectStandardOutput = True
        'emu.StartInfo.RedirectStandardError = True
        Try
            emu.Start()
        Catch
            MsgBox("Error starting " + (emuPath), MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical)
        End Try
        'tb.AppendText(emu.StandardOutput.ReadToEnd())
        'tb.AppendText(emu.StandardError.ReadToEnd())

        'emu.WaitForExit(500)
        emu.Dispose()
    End Sub

End Class
